#include "thread_pool.h"

void *mytask(void *arg1, void *arg2)
{
	long n = (long)arg1;
	printf("第二个参数是 is %s\n", (char *)arg2);
	printf("线程id为[%ld]的线程准备工作 %ld 秒...\n",
		pthread_self(), n);

	sleep(n);

	printf("线程id为[%ld]的线程工作 %ld 秒结束了******************\n",
		pthread_self(), n);

	return NULL;
}

void *count_time(void *arg)
{
	int i = 0;
	while(1)
	{
		sleep(1);
		printf("当前是第%d秒\n", ++i);
	}
}

int main(void)
{
	pthread_t a;
	
	//只是用来创建一天用于计时的线程，跟线程池一点关系都没有
	pthread_create(&a, NULL, count_time, NULL);

	thread_pool *pool = malloc(sizeof(thread_pool));
	
	// 1, 初始化线程池，并且一开始就有两条线程
	init_pool(pool, 2);

	// 2, 任务投放
	printf("投放3个任务\n");
	add_task(pool, mytask, (void *)((rand()%10)*1L), (void *)("hhhhh"));
	add_task(pool, mytask, (void *)((rand()%10)*1L), (void *)("xixixi"));
	add_task(pool, mytask, (void *)((rand()%10)*1L), (void *)("heiheihei"));

	// 3, 检查当前线程数量
	printf("当前线程数量为：%d\n",
					remove_thread(pool, 0));
	sleep(9);
	
	//增加线程数量
	add_thread(pool, 2);

	sleep(5);

	// 6, 删除线程（随意，因为删除线程池的时候，会全部删除线程）
	printf("删除3条线程，当前线程数还剩: %d\n",
						remove_thread(pool, 3));

	// 7, 删除线程池
	destroy_pool(pool);
	
	//8、释放线程池资源
	free(pool);
	
	return 0;
}
